package org.example.javachatroom.controller;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class TestWebSocketControler extends TextWebSocketHandler{
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        //这个方法会在websocket连接建立成功后被自动调用
        System.out.println("TestController 连接成功！");
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        //这个方法是在websocket连接异常的时候被自动调用的
        System.out.println("TestController 连接异常！");

    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        //这个方法是在websocket连接关闭时候被自动调用的
        System.out.println("TestController 连接关闭");

    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        //这个方法是在websocket收到消息的时候被自动调用的
        System.out.println("TestController 收到消息！"+message.toString());
        //session是个会话，里面记录了通信双方是谁（session中持有了websocket的通信连接）
        session.sendMessage(message);
    }
}
